package com.enjoy.game.xcard.sprite;

import java.util.ArrayList;

import org.andengine.entity.Entity;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.font.IFont;
import org.andengine.opengl.shader.ShaderProgram;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.opengl.vbo.DrawType;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import android.content.Context;

public class DeckBuilder {
	static final int MAX_CARD_PER_ROW = 4;
	
	Context mContext;
	Deck mDeck;

	
	public DeckBuilder(Context context){
		mContext = context;
		mDeck = new Deck();
		
	}
	
	
	CardInfo creatCardInfo(int cardID, String name, String imgurl, 
			int topDC, int rightDC, int bottomDC, int leftDC){
		CardInfo cardInfo = new CardInfo(cardID, name, imgurl);
		cardInfo.setTopDC(topDC).setRightDC(rightDC).setBottomDC(bottomDC).setLeftDC(leftDC);
		return cardInfo;
	}
	
	ArrayList<CardInfo> creatCardInfoList(){
		ArrayList<CardInfo>  cardInfoList = new ArrayList<CardInfo> (10);
		cardInfoList.add(creatCardInfo(1, "xcard_1", "1.png", 10, 90, 20, 80));
		cardInfoList.add(creatCardInfo(2, "xcard_2", "2.png", 20, 80, 30, 70));

		cardInfoList.add(creatCardInfo(3, "xcard_3", "3.png", 30, 70, 40, 60));
		cardInfoList.add(creatCardInfo(4, "xcard_4", "4.png", 40, 60, 50, 50));
		cardInfoList.add(creatCardInfo(5, "xcard_5", "5.png", 50, 50, 60, 40));
		cardInfoList.add(creatCardInfo(6, "xcard_6", "6.png", 60, 40, 70, 30));
		cardInfoList.add(creatCardInfo(7, "xcard_7", "7.png", 70, 30, 80, 20));
		cardInfoList.add(creatCardInfo(8, "xcard_8", "8.png", 80, 20, 90, 10));
		cardInfoList.add(creatCardInfo(9, "xcard_9", "9.png", 90, 10, 80, 20));
		cardInfoList.add(creatCardInfo(10, "xcard_10", "10.png", 50, 70, 40, 30));
		return cardInfoList;
	}
	
	public int loadCards(BitmapTextureAtlas deckTexureAtlas, IFont font, CellLayout  cardEntity, VertexBufferObjectManager pVertexBufferObjectManager){
		mDeck.setCardInfoList(creatCardInfoList());
		return loadCards(deckTexureAtlas, font, cardEntity, pVertexBufferObjectManager, mDeck.getCardInfoList() );
	}
	
	public int loadCards(BitmapTextureAtlas deckTexureAtlas, IFont font, CellLayout  cardEntity, VertexBufferObjectManager pVertexBufferObjectManager, ArrayList<CardInfo> cardInfos){
		int count = 0;
		int row = 0;
		int column = 0;
		for(CardInfo cardInfo : cardInfos){
			row = count/MAX_CARD_PER_ROW;
			column = count%MAX_CARD_PER_ROW;
			
			TiledTextureRegion textureReg =  BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(deckTexureAtlas, mContext, 
							cardInfo.getImgUrl(), column* XCard.CARD_TEXTURE_WIDTH, row*XCard.CARD_TEXTURE_HEIGHT, 2, 1);
			XCard card = new XCard(0, 0, textureReg,  pVertexBufferObjectManager , font, cardInfo);
			cardEntity.putCard(card);
			count ++;
		}
		deckTexureAtlas.load();
		
		return 0;
	}
	
	
}
